home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / xpt_xdr.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  7KB  |  212 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /*
  39.  * Basic APIs for streaming typelib structures to/from disk.
  40.  */
  41.  
  42. #ifndef __xpt_xdr_h__
  43. #define __xpt_xdr_h__
  44.  
  45. #include "xpt_struct.h"
  46.  
  47. PR_BEGIN_EXTERN_C
  48.  
  49. typedef struct XPTState         XPTState;
  50. typedef struct XPTDatapool      XPTDatapool;
  51. typedef struct XPTCursor        XPTCursor;
  52.  
  53. /* Opaque type, for internal use */
  54. typedef struct XPTHashTable     XPTHashTable;
  55.  
  56. extern XPT_PUBLIC_API(PRBool)
  57. XPT_DoString(XPTArena *arena, XPTCursor *cursor, XPTString **strp);
  58.  
  59. extern XPT_PUBLIC_API(PRBool)
  60. XPT_DoStringInline(XPTArena *arena, XPTCursor *cursor, XPTString **strp);
  61.  
  62. extern XPT_PUBLIC_API(PRBool)
  63. XPT_DoCString(XPTArena *arena, XPTCursor *cursor, char **strp);
  64.  
  65. extern XPT_PUBLIC_API(PRBool)
  66. XPT_DoIID(XPTCursor *cursor, nsID *iidp);
  67.  
  68. extern XPT_PUBLIC_API(PRBool)
  69. XPT_Do64(XPTCursor *cursor, PRInt64 *u64p);
  70.  
  71. extern XPT_PUBLIC_API(PRBool)
  72. XPT_Do32(XPTCursor *cursor, PRUint32 *u32p);
  73.  
  74. extern XPT_PUBLIC_API(PRBool)
  75. XPT_Do16(XPTCursor *cursor, PRUint16 *u16p);
  76.  
  77. extern XPT_PUBLIC_API(PRBool)
  78. XPT_Do8(XPTCursor *cursor, PRUint8 *u8p);
  79.  
  80. extern XPT_PUBLIC_API(PRBool)
  81. XPT_DoHeaderPrologue(XPTArena *arena, XPTCursor *cursor, XPTHeader **headerp, PRUint32 * ide_offset);
  82. extern XPT_PUBLIC_API(PRBool)
  83. XPT_DoHeader(XPTArena *arena, XPTCursor *cursor, XPTHeader **headerp);
  84.  
  85. typedef enum {
  86.     XPT_ENCODE,
  87.     XPT_DECODE
  88. } XPTMode;
  89.  
  90. typedef enum {
  91.     XPT_HEADER = 0,
  92.     XPT_DATA = 1
  93. } XPTPool;
  94.  
  95. struct XPTState {
  96.     XPTMode          mode;
  97.     PRUint32         data_offset;
  98.     PRUint32         next_cursor[2];
  99.     XPTDatapool      *pool;
  100.     XPTArena         *arena;
  101. };
  102.  
  103. struct XPTDatapool {
  104.     XPTHashTable     *offset_map;
  105.     char             *data;
  106.     PRUint32         count;
  107.     PRUint32         allocated;
  108. };
  109.  
  110. struct XPTCursor {
  111.     XPTState    *state;
  112.     XPTPool     pool;
  113.     PRUint32    offset;
  114.     PRUint8     bits;
  115. };
  116.  
  117. extern XPT_PUBLIC_API(XPTState *)
  118. XPT_NewXDRState(XPTMode mode, char *data, PRUint32 len);
  119.  
  120. extern XPT_PUBLIC_API(PRBool)
  121. XPT_MakeCursor(XPTState *state, XPTPool pool, PRUint32 len, XPTCursor *cursor);
  122.  
  123. extern XPT_PUBLIC_API(PRBool)
  124. XPT_SeekTo(XPTCursor *cursor, PRUint32 offset);
  125.  
  126. extern XPT_PUBLIC_API(void)
  127. XPT_DestroyXDRState(XPTState *state);
  128.  
  129. /* Set file_length based on the data used in the state.  (Only ENCODE.) */
  130. extern XPT_PUBLIC_API(PRBool)
  131. XPT_UpdateFileLength(XPTState *state);
  132.  
  133. /* returns the length of the specified data block */
  134. extern XPT_PUBLIC_API(void)
  135. XPT_GetXDRDataLength(XPTState *state, XPTPool pool, PRUint32 *len);
  136.  
  137. extern XPT_PUBLIC_API(void)
  138. XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, PRUint32 *len);
  139.  
  140. /* set or get the data offset for the state, depending on mode */
  141. extern XPT_PUBLIC_API(void)
  142. XPT_DataOffset(XPTState *state, PRUint32 *data_offsetp);
  143.  
  144. extern XPT_PUBLIC_API(void)
  145. XPT_SetDataOffset(XPTState *state, PRUint32 data_offset);
  146.  
  147. extern XPT_PUBLIC_API(PRUint32)
  148. XPT_GetOffsetForAddr(XPTCursor *cursor, void *addr);
  149.  
  150. extern XPT_PUBLIC_API(PRBool)
  151. XPT_SetOffsetForAddr(XPTCursor *cursor, void *addr, PRUint32 offset);
  152.  
  153. extern XPT_PUBLIC_API(PRBool)
  154. XPT_SetAddrForOffset(XPTCursor *cursor, PRUint32 offset, void *addr);
  155.  
  156. extern XPT_PUBLIC_API(void *)
  157. XPT_GetAddrForOffset(XPTCursor *cursor, PRUint32 offset);
  158.  
  159. /* all data structures are big-endian */
  160.  
  161. #if defined IS_BIG_ENDIAN
  162. #  define XPT_SWAB32(x) x
  163. #  define XPT_SWAB16(x) x
  164. #elif defined IS_LITTLE_ENDIAN
  165. #  define XPT_SWAB32(x) (((x) >> 24) |                                        \
  166.              (((x) >> 8) & 0xff00) |                                          \
  167.              (((x) << 8) & 0xff0000) |                                        \
  168.              ((x) << 24))
  169. #  define XPT_SWAB16(x) (((x) >> 8) | ((x) << 8))
  170. #else
  171. #  error "unknown byte order"
  172. #endif
  173.  
  174. /*
  175.  * If we're decoding, we want to read the offset before we check
  176.  * for already-decoded values.
  177.  *
  178.  * Then we check for repetition: CheckForRepeat will see if we've already
  179.  * encoded/decoded this value, and if so will set offset/addr correctly
  180.  * and make already be true.  If not, it will set up the cursor for
  181.  * encoding (reserve space) or decoding (seek to correct location) as
  182.  * appropriate.  In the encode case, it will also set the addr->offset
  183.  * mapping.
  184.  */
  185.  
  186. #define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already)           \
  187.     XPTMode mode = cursor->state->mode;                                       \
  188.     if (!(mode == XPT_ENCODE || XPT_Do32(cursor, &new_curs.offset)) ||        \
  189.         !CheckForRepeat(cursor, (void **)addrp, pool,                         \
  190.                         mode == XPT_ENCODE ? size : 0u, &new_curs,            \
  191.                         &already) ||                                          \
  192.         !(mode == XPT_DECODE || XPT_Do32(cursor, &new_curs.offset)))          \
  193.         return PR_FALSE;                                                      \
  194.     if (already)                                                              \
  195.         return PR_TRUE;                                                       \
  196.  
  197. #define XPT_PREAMBLE_NO_ALLOC(cursor, addrp, pool, size, new_curs, already)   \
  198.   {                                                                           \
  199.     XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already)               \
  200.   }
  201.  
  202. #define XPT_ERROR_HANDLE(arena, free_it)                                      \
  203.  error:                                                                       \
  204.     if (cursor->state->mode == XPT_DECODE)                                    \
  205.     XPT_FREEIF(arena, free_it);                                               \
  206.     return PR_FALSE;
  207.  
  208.  
  209. PR_END_EXTERN_C
  210.  
  211. #endif /* __xpt_xdr_h__ */
  212.